

//
//
// adventure screen handler for the Tavern Object
//
// L. Dean Gibson II  2/7/01
//

#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adv_tavern.h"

#include "adv_object_model.h"
#include "adventure_frame.h"
#include "adventure_map.h" 
#include "adventure_sounds.h"
#include "army.h"
#include "basic_dialog.h"
#include "counted_ptr.h"
#include "external_string.h"
#include "hero.h"
#include "hero_class_properties.h"
#include "hire_hero_dialog.h"
#include "music.h"
#include "object_registration.h"
#include "player.h"
#include "simple_dialog.h"
#include "town_type.h"

static t_object_registration<t_adv_tavern> k_registration( k_adv_object_tavern );

// -------------------------------------------------------
// tavern object constructor
// -------------------------------------------------------
t_adv_tavern::t_adv_tavern( std::string const& model_name )
			  : t_stationary_adventure_object( model_name )
{
	m_closed = false;
	m_closed_days = 0;

}

// -------------------------------------------------------
// activate trigger for the Tavern adventure map object
// -------------------------------------------------------
void t_adv_tavern::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                               t_direction direction, t_adventure_frame* frame )
{
	t_counted_ptr<t_hire_hero_dialog> dialog;
	t_player *						  player = army->get_owner();
	int                               funds = player->get_funds()[k_gold];
	int                               hire_cost[k_hero_class_basic_count];

	get_hero_hiring_cost( k_town_none, hire_cost );

	// check the 7 day limit on tavern visitation, can only come around once a week
	if (m_closed) 
	{
		t_basic_dialog*          dialog;
		std::string              text;

		dialog = new t_basic_dialog;
		text = get_text( "empty" );
		dialog->set_text( text );

		dialog->add_ok_button();

		dialog->open( get_dialog_bottom( point ), true, k_align_bottom );
		t_sound_cache music = get_music_playing();
		stop_music();
		get_dialog_sound( k_dialog_sound_tavern )->play( get_sound_volume() );
		dialog->run_modal();
		play_music( music );

		return;
	}

	// army is full, display appropriate message and bail out
	if ( !army->get_creatures().can_add_hero() )  {
		t_basic_dialog*          dialog;
		std::string              text;

		dialog = new t_basic_dialog;
		text = get_text( "denied" );
		dialog->set_text( text );

		dialog->add_ok_button();

		dialog->open( get_dialog_bottom( point ), true, k_align_bottom );
		t_sound_cache music = get_music_playing();
		stop_music();
		get_dialog_sound( k_dialog_sound_tavern )->play( get_sound_volume() );
		dialog->run_modal();
		play_music( music );
		
		return;
	}

	dialog = new t_hire_hero_dialog( hire_cost, funds, *get_map() );
	//dialog->set_cursor( get_cursor( false ));
	if (!dialog->run_modal())
		return;

	// hire hero.
	t_default_hero data = dialog->get_data();
	t_hero*        hero = get_map()->create_hero( data, get_alignment( data.hero_class ) );

	player->spend( k_gold, hire_cost[data.hero_class] );
	army->get_creatures().add( hero );
	frame->update();

	m_closed = true; // close the tavern for 7 days
}

//
// a tavern processing the new day event
//
void t_adv_tavern::process_new_day()
{
	// has this tavern been closed? and if so has it been so for 7 or more days?
	if (m_closed && m_closed_days >= 7) 
	{ 
		m_closed = false; // re-open it
		m_closed_days = 0;
	} 
	else 
	{
		m_closed_days++;
	}
}

// -------------------------------------------------------
// tavern as it appears on the adventure map
// -------------------------------------------------------
bool t_adv_tavern::write( std::streambuf& stream ) const
{
	put<bool>( stream, m_closed );
	put<t_uint8>( stream, m_closed_days );
	return true;
}

// -------------------------------------------------------
// tavern as it appears on the adventure map
// -------------------------------------------------------
bool t_adv_tavern::read( std::streambuf&						stream, 
						 t_qualified_adv_object_type const&	type,
						 int									version )
{
	m_closed = get<bool>( stream );
	m_closed_days = get<t_uint8>( stream );
	return true;
}
